<template>
    <el-dialog
            title="导出"
            :visible.sync="show"
            width="50%"
            :lock-scroll="true"
    >
        <div style="overflow: auto; height: 50vh">
            <el-tree
                    :data="list"
                    show-checkbox
                    node-key="id"
                    ref="tree"
                    :props='{"label":"text"}'
                    :default-expand-all="true">
            </el-tree>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button @click="show = false">取 消</el-button>
            <el-button type="primary" @click="confirm">确 定</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import download from "../libs/download";
    import {formatDate} from "../libs/date";

    export default {
        name: "FormExport",
        data(){
            return{
                show: false,
                list: []
            }
        },
        methods: {
            open() {
                this.list = [];
                this.show = true;
                $.get("/form/listex", {type: "form"}, (msg) => {
                    this.list = msg.data||[];
                });
            },
            getParent(id){
                var node = this.$refs.tree.getNode(id)

                let list = []
                list.push(node.key)
                if(node.parent.key != null){
                    list.push(this.getParent(node.parent.key))
                    return list.flat()
                }else{
                    return list.flat()
                }
                // list.forEach(val=>{
                //     if(val.pid != null){
                //
                //     }
                // })
            },
            confirm(){
                this.show = false;
                var set = new Set();
                this.$refs.tree.getCheckedNodes().forEach((val)=>{
                    set.add(val.id)
                    this.getParent(val.id).forEach(e => {
                        set.add(e);
                    });
                });
                if(set.size === 0){
                    this.$notify.info("请选择要导出的对象")
                }
                var ids = [...set].join(",")
                var url = window.Env.ApiUrl;
                console.log(`${url}/form/export?bpm-token=${localStorage.getItem("bpm-token")}&ids=${ids}&type=form`)
                download(`${url}/form/export?bpm-token=${localStorage.getItem("bpm-token")}&ids=${ids}&type=form`, `表单导出-${formatDate(new Date(), "yyyy-MM-dd")}.data`);

            }
        },
        mounted() {

        }
    }

</script>

<style scoped>

</style>